<?php


namespace App\Http\Middleware\Backend;

use App\Models\ManageAdminModel;
use Closure;
use Illuminate\Http\Request;

class CheckSafePwd extends Middleware
{
    public function handle(Request $request, Closure $next)
    {
        if(!$this->checkSafePwd($request)){
            return $this->responseMethod([
                'status'=>0,
                'msg'=>'安全密码错误',
                'url'=>url()->previous(),
            ],200);
        }

        return $next($request);
    }

    private function checkSafePwd(Request $request)
    {
        $pwd = $request->post('safe_pwd',false);
        if(!$pwd){
            return false;
        }
        $mgr_id = ManageAdminModel::init()->getLoginId();
        if(!ManageAdminModel::init()->checkSafePwd($pwd,$mgr_id)){
            return false;
        }

        return true;
    }
}